<template>
	<div class="register_page">
		<van-nav-bar :title="$t('home.resetPassword')" placeholder left-arrow @click-left="onClickLeft" safe-area-inset-top
			:border="false" />
		<div class="page_cont">
			<el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="0" class="demo-ruleForm">
				<el-form-item prop="old_password">
					<el-input v-model="ruleForm.old_password" :placeholder="$t('login.enenterOldPassword')" show-password></el-input>
				</el-form-item>
				<el-form-item prop="new_password">
					<el-input v-model="ruleForm.new_password" :placeholder="$t('login.enenterNewPassword')" show-password></el-input>
				</el-form-item>
				<el-form-item prop="secondary_password">
					<el-input v-model="ruleForm.secondary_password" :placeholder="$t('login.enenterNewPasswordAgain')" show-password></el-input>
				</el-form-item>
				<el-button class="but" @click="handleRest()">{{$t('home.resetPassword')}}</el-button>
			</el-form>
		</div>
	</div>
</template>

<script>
	import {
		userAmendPassword
	} from '@/api/home'
	import Cookie from 'vue-cookies'
	export default {
		props: {},
		components: {},
		data() {
			return {
				type: 'email',
				loading: false,
				intimer: null,
				dwonCodeText: '',
				areaList: [], // 地区区号
				ruleForm: {
					old_password: '',
					new_password: '',
					secondary_password: '',
				},

			}
		},
		computed: {
			rules() {
				var validatePass2 = (rule, value, callback) => {
					if (value === '') {
						callback(new Error(this.$t('login.pleaseEnterPasswordAgain')))
					} else if (value !== this.ruleForm.new_password) {
						callback(new Error(this.$t('login.thePasswordNot')))
					} else {
						callback()
					}
				}
				return {
					old_password: [{
						required: true,
						message: this.$t('login.passwordValidate'),
						trigger: 'change'
					}],
					new_password: [{
						required: true,
						message: this.$t('login.passwordValidate'),
						trigger: 'change'
					}],
					secondary_password: [{
						required: true,
						validator: validatePass2,
						trigger: 'change'
					}],
				}
			},
		},
		watch: {},
		mounted() {},
		methods: {
			onClickLeft() {
				this.$router.go(-1)
			},
			handleRest() {
				this.$refs.ruleForm.validate(async (valid) => {
					if (valid) {
						const res = await userAmendPassword({
							...this.ruleForm,
						})
						if (res && res.code == 1) {
							this.$message.success(res.msg)
							localStorage.clear('LOGIN_USER_INFO')
							localStorage.clear('TOKEN')
							this.$store.commit('onChangeUserInfo', {})
							this.$router.replace('/login')
						} else {
							this.$message.error(res.msg)
						}
					} else {
						console.log('error submit!!');
						return false;
					}
				});
			}
		}
	}
</script>

<style scoped lang="less">
	.register_page {
		position: relative;
		height: 100vh;

		::v-deep .van-nav-bar {
			background-color: #1B232E;

			.van-icon {
				color: #fff;
				font-size: 18px;
			}

			.van-nav-bar__title {
				color: #fff;
			}
		}

		.page_cont {
			padding: 25px 15px 5px;
			
			::v-deep .el-input__inner {
				background-color: #12171D;
				height: 50px;
				border: 0;
				color: #fff;
			}
			
			.but {
				width: 100%;
				height: 46px;
				background-color: #137EFE;
				border: 0;
				border-radius: 10px;
				color: #fff;
				margin-top: 40px;
			}
		}
	}
</style>